define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classroom/test_log/index' + location.search,
                    add_url: 'classroom/test_log/add',
                    edit_url: 'classroom/test_log/edit',
                    check_url: 'classroom/test_log/check',
                    detail_url: 'classroom/test_log/detail',
                    del_url: 'classroom/test_log/del',
                    multi_url: 'classroom/test_log/multi',
                    import_url: 'classroom/test_log/import',
                    table: 'classroom_test_log',
                }
            });

            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#total_num").text(data.total);
                $("#no_num").text(data.extend.no_num);
                $("#yes_num").text(data.extend.yes_num);
            });

            $(document).on("click",".btn_pg",function () {
                let id = $(this).attr('data-id');
                Fast.api.open("classroom/test_log/check?id="+id,"批改试卷",{});
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'score', title: __('Score'),sortable: true},
                        {
                            field: 'status', title: __('Status'), sortable: true,
                            searchList: {"0":__('Status 0'),"1":__('Status 1')},
                            formatter: function (value, row, index) {
                                if(value === 0){
                                    return '<bottom class="btn btn-xs btn_pg btn-success" title="批改" style="margin-right: 5px;" data-id="'+row.id+'"><i class="fa fa-edit"></i> 批改</bottom>';
                                }
                                return '<span class="text-info" style="margin-right: 5px;">'+__('Status 1')+'</span>';
                            }
                        },
                        {
                            field: 'is_pass', title: __('Is_pass'), searchList: {"0":__('Is_pass 0'),"1":__('Is_pass 1')}, custom:{1: 'info', 0:'danger'},
                            formatter: function (value, row, index) {
                                if(row.status === 0){
                                    return '<span class="text-warning" style="margin-right: 5px;">待批阅</span>';
                                } else if(value === 1){
                                    return '<span class="text-info" style="margin-right: 5px;">及格</span>';
                                } else if(value === 0){
                                    return '<span class="text-danger" style="margin-right: 5px;">不及格</span>';
                                }
                                return value;
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-file-text-o',
                                    url: 'classroom/test_log/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status ? true : false;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        check: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});